package com.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.DepDao;
import com.pojo.Dep;
import com.service.IDepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@SuppressWarnings("All")
@Service
public class DepServiceImpl extends ServiceImpl<DepDao, Dep> implements IDepService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DepDao depDao;


    /**
     * 先查缓存，缓存里面有，则返回
     *      * 如果缓存不存在，则查数据库，再存缓存
     * @return
     */
    @Cacheable(value = "dep",key = "'findALl'")
    public List findALl(){
        List list=depDao.selectList(null);
        return list;
    }


    @CacheEvict(value = "dep",key = "#id",allEntries = true,beforeInvocation = true)
    public int delById(int id){
        return depDao.deleteById(id);
    }

    @Cacheable(value = "dep",key = "#id")
    public Dep findById(int id){
        return depDao.selectById(id);
    }
    @CacheEvict(value = "dep",key = "#id",allEntries = true,beforeInvocation = true)
    @CachePut(value = "dep",key = "#dep.depId")
    public int updateDep(Dep dep){
        return depDao.updateById(dep);
    }

    @Cacheable(value = "dep",key = "'searchDep'")
    public List searchDep(String depName,Integer depId){
        QueryWrapper q=new QueryWrapper();
        if (depId!=null){
            q.eq("depId",depId);
        }
        if (depName!=null&& depName!=""){
            q.eq("depName",depName);
        }
        List list=depDao.selectList(q);
        return list;
    }


}
